<template>
  <div>
    <!-- 面包屑 breadcrumb -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card -->
    <el-card>
      <template #header>
        <div class="header">
          <span>共{{ total }}条记录</span>
          <!-- 1添加功能 -->
          <el-button
            round
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="add"
            >添加面经</el-button
          >
        </div>
      </template>
      <!-- table -->
      <el-table :data="rows" style="width: 100%">
        <el-table-column label="标题" prop="stem"></el-table-column>
        <el-table-column label="作者" prop="creator"></el-table-column>
        <el-table-column label="点赞" prop="likeCount"></el-table-column>
        <el-table-column label="浏览数" prop="views"></el-table-column>
        <el-table-column label="更新时间" prop="createdAt"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <i class="el-icon-view" @click="preview(scope.row.id)"></i>
            <!-- 6:编辑功能 -->
            <i class="el-icon-edit-outline" @click="edit(scope.row.id)"></i>
            <!-- 1删除功能 5:点击删除 把form表单的内容获得一次，然后再渲染-->
            <i class="el-icon-delete" @click="del(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="params.pageSize"
        :current-page="params.current"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 1:加入抽屉 改大小，标题不能写死-->
    <!-- 3：抽屉有个方法，关闭就把里面的内容清空一下 -->
    <el-drawer
      :title="title"
      size="50%"
      :visible.sync="drawer"
      direction="rtl"
      @close="closeFn"
    >
      <el-form
        v-if="title !== '预览面经'"
        :model="form"
        :rules="rules"
        label-width="70px"
        ref="formRef"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- <el-input v-model="form.content" type="textarea"></el-input> 校验部分-->
          <quill-editor
            v-model="form.content"
            @blur="$refs.formRef.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <!-- 3：点击内容把数据给到data，然后重新渲染一遍 -->
          <!-- 4:点击确定添加内容 -->
          <el-button type="primary" @click="saveAdd">确认</el-button>
          <el-button type="default">取消</el-button>
        </el-form-item>
      </el-form>
      <div v-else>
        <h3>{{ form.stem }}</h3>
        <el-divider></el-divider>
        <p v-html="form.content"></p>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// 2写富文本框
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import {
  addArticle,
  getArticleList,
  delArticle,
  editArticle,
  updateArticle
} from '@/api/article'
export default {
  name: 'article-page',
  data() {
    return {
      params: {
        current: 1,
        pageSize: 10
      },
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      },
      rows: null,
      total: null,
      drawer: false,
      title: ''
    }
  },
  created() {
    this.getArticle()
  },

  methods: {
    async getArticle() {
      const res = await getArticleList(this.params)
      console.log(res)
      this.rows = res.data.rows
      this.total = res.data.total
    },
    // 2:添加功能
    add() {
      // 解除点编辑又取消再点添加，以上来就有内容
      this.form = {
        stem: '',
        content: ''
      }
      this.title = '添加面经'
      this.drawer = true
    },
    // 3：关闭抽屉让内容重置
    closeFn() {
      this.$refs.formRef.resetFields()
    },
    // 4:点击确定添加新的面经
    async saveAdd() {
      // 添加前先校验下 因为validate方法返回的是promise对象所以前面写await
      await this.$refs.formRef.validate()
      // 6:以title是否为添加面经为条件，
      if (this.title === '修改面经') {
        // 编辑 创建一个空的 接口只要三个数据
        const form = {}
        form.id = this.form.id
        form.stem = this.form.stem
        form.content = this.form.content

        await updateArticle(form)
        // console.log(res)
        this.$message.success('修改成功')
        // 重新
      } else {
        // 添加
        // 拿到数据
        await addArticle(this.form)
        // 添加成功
        this.$message.success('添加成功')
        // console.log(res) //
      }

      // 重新渲染页面
      this.getArticle()
      // 关闭抽屉
      this.drawer = false
    },
    // 5:做删除功能
    async del(id) {
      // 做个提示功能 提示完如果取消则会有错误
      try {
        await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // console.log(id)
        await delArticle(id)
        // 删除成功
        this.$message.success('删除成功')
        // console.log(res)
        // 拿到数据后，重新渲染数据
        this.getArticle()
      } catch (error) {
        console.log(error)
      }
    },
    // 6:编辑功能 获取到单个面经
    async edit(id) {
      this.title = '修改面经'
      const res = await editArticle(id)
      console.log(res.data)
      // 把数据给到form
      this.form = res.data
      // 打开抽屉
      this.drawer = true

      // 拿到数据把数据给到，但是点确认的时候，要判断是编辑还是添加
    },
    // 7:预览
    async preview(id) {
      this.title = '预览面经'
      const res = await editArticle(id)
      console.log(res)
      this.form.stem = res.data.stem
      this.form.content = res.data.content
      // 打开抽屉
      this.drawer = true
    }
  },
  components: {
    quillEditor
  }
}
</script>

<style scoped lang="scss">
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 1;
  }
  i {
    padding: 0 10px;
  }
  .el-pagination {
    text-align: center;
    margin-top: 20px;
  }
}
::v-deep .ql-editor {
  // 因为内容不知道多少，用最小高度最合适
  min-height: 200px;
}
</style>
